package com.oa.srms.apps.actions.organization;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import net.sf.json.JSONObject;

import com.oa.srms.apps.actions.BasicAction;
import com.oa.srms.apps.business.organization.ISectionCategoryService;
import com.oa.srms.apps.business.organization.ISectionInfoService;
import com.oa.srms.apps.constants.IDEDStates;
import com.oa.srms.apps.entity.organization.SectionCategoryValue;
import com.oa.srms.apps.entity.organization.SectionInfoValue;

@SuppressWarnings("serial")
public class SectionInfoAction extends BasicAction {

	private List<SectionInfoValue> infoList;
	private List<SectionCategoryValue> categoryList;
	@Autowired
	private ISectionCategoryService categoryService;
	@Autowired
	private ISectionInfoService infoService;
	private List<SectionInfoValue> allInfoList;


	private String sectionIds;
	private Integer sectionId;
	private String sectionInfoJsonString;
	
	private String str;

	public String execute() throws Exception {
		categoryList = categoryService.querySectionCategory(IDEDStates.ENABLE);
		allInfoList = infoService.queryAllSectionInfo();
		infoList = infoService.querySectionInfo(null, IDEDStates.ENABLE);
		count = allInfoList.size();
		return "show";
	}
	
	public String delete() throws Exception{
		if(infoService.deleteSectionInfo(sectionIds)){
			return "delete";
		}
		return "false";
	}
	
	public String toDisable() throws Exception{
		if(infoService.toDisable(sectionId)){
			return "toDisable";
		}
		return "false";
	}
	
	public String toEnable() throws Exception{
		if(infoService.toEnable(sectionId)){
			return "toEnable";
		}
		return "false";
	}
	
	public String add() throws Exception{
		Integer sectionId = null;
		String sectionNo = null;
		String sectionName = null;
		Integer categoryId = null;
		try {
			JSONObject json =   JSONObject.fromObject(sectionInfoJsonString);
			if(!json.get("sectionId").equals("")){
				sectionId = json.getInt("sectionId");
			}
			if(!json.get("sectionNo").equals("")){
				sectionNo= json.getString("sectionNo");
			}
			if(!json.get("sectionName").equals("")){
				sectionName = json.getString("sectionName");;
			}
			if(!json.get("sectionCategoryId").equals("")){
				categoryId = json.getInt("sectionCategoryId");
			}
			if(sectionId == null){
				if(infoService.addSectionInfo(categoryId, sectionNo, sectionName)>0){
					str = "1";
				}else{
					str = "-1";
				}
			}else{ 
				if(infoService.modifySectionInfo(sectionId, categoryId, sectionNo, sectionName)){
					str = "1";
				}else{
					str = "-1";
				}
			}
			response.setContentType("text/html; charset=UTF-8");
			response.setHeader("Cache-Control", "no-cache");
			response.getWriter().write(str);
		} catch (Exception e) {
			return "false";
		}
		return null;
	}
	
	public String addCategory() throws Exception{
		String categoryName = null;
		try {
			JSONObject json =   JSONObject.fromObject(sectionInfoJsonString);
			if(!json.get("categoryName").equals("")){
				categoryName = json.getString("categoryName");
			}
			if(sectionId == null){
				categoryService.addSectionCategory(categoryName);
			}
		} catch (Exception e) {
			return "false";
		}
		return null;
	}
	
	
	public void setStr(String str) {
		this.str = str;
	}
	
	public String getStr() {
		return str;
	}

	public String getSectionIds() {
		return sectionIds;
	}

	public void setSectionIds(String sectionIds) {
		this.sectionIds = sectionIds;
	}

	public Integer getSectionId() {
		return sectionId;
	}

	public void setSectionId(Integer sectionId) {
		this.sectionId = sectionId;
	}

	public String getSectionInfoJsonString() {
		return sectionInfoJsonString;
	}

	public void setSectionInfoJsonString(String sectionInfoJsonString) {
		this.sectionInfoJsonString = sectionInfoJsonString;
	}

	public List<SectionInfoValue> getInfoList() {
		return infoList;
	}

	public void setInfoList(List<SectionInfoValue> infoList) {
		this.infoList = infoList;
	}

	public List<SectionCategoryValue> getCategoryList() {
		return categoryList;
	}

	public void setCategoryList(List<SectionCategoryValue> categoryList) {
		this.categoryList = categoryList;
	}

	public List<SectionInfoValue> getAllInfoList() {
		return allInfoList;
	}

	public void setAllInfoList(List<SectionInfoValue> allInfoList) {
		this.allInfoList = allInfoList;
	}
	

}
